using System.Security.Principal;
using System.Threading;
using TaskKernel.Domain.Services.Role;

namespace TaskKernel.Domain.Services.Action
{
    public class TaskServiceByRoleInDepartment : ITaskService
    {
        private readonly ITaskAction _taskAction;

        public TaskServiceByRoleInDepartment(ITaskAction taskAction, Department department, Account account)
        {
            _taskAction = taskAction;
            var roleInDepartment = new RoleInDepartment(department, account);
            Role = roleInDepartment.GetRole();
        }

        public void ChangePrincipal()
        {
            var wIdent = WindowsIdentity.GetCurrent();
            var customPrincipal = new CustomPrincipal(wIdent, Role);
            Thread.CurrentPrincipal = customPrincipal;
        }
        public RoleEnum Role { get; protected set; }

        #region ITaskService Members

        public virtual ITaskAction GetAction()
        {
            ChangePrincipal();
            return _taskAction;
        }

        #endregion
    }
}